package org.goobs.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class StringManipulator {

	public static int findIn(String haystack, String needle) {
		char[] transition = needle.toCharArray();
		char[] needleChar = needle.toCharArray();
		char[] haystackChar = haystack.toCharArray();
		int[] epsilon = new int[needle.length()];

		for (int i = 0; i < needle.length(); i++) {
			epsilon[i] = epsilon(needleChar, transition, epsilon,
					needleChar[i], i);
		}
//		print(epsilon);

		int state = 0;
		for (int index = 0; index < haystackChar.length; index++) {
			char c = haystackChar[index];
			while (transition[state] != c && state != 0) {
				state = epsilon[state-1];
//				System.out.println("   >" + state);
			}
//			System.out.print(c + " : " + state);
			if (transition[state] == c) {
				state++;
			}
//			System.out.println(" -> " + state);
			if (state >= needle.length()) {
				return index - needle.length() + 1;
			}
		}
		return -1;
	}

	private static int epsilon(char[] needle, char[] transition, int[] epsilon,
			char nextChar, int index) {
//		System.out.println(index + " " + nextChar);
		if (index <= 0) {
			return 0;
		}
		int lastEpsilon = epsilon[index - 1];
		if (transition[lastEpsilon] == nextChar) {
			return lastEpsilon+1;
		} else {
			return epsilon(needle, transition, epsilon, nextChar, lastEpsilon);
		}
	}
	
	private static void print(int[] epsilon){
		System.out.print("[ ");
		for(int i : epsilon){
			System.out.print(i + " ");
		}
		System.out.println("]");
	}

	public static void main(String[] args) {
		// char[] chars = {'a', 'n', 'b' };
		// StringBuilder b = new StringBuilder();
		// for(int i=0; i<10000000; i++){
		// b.append(chars[(new Random()).nextInt(chars.length)]);
		// }
		// b.append("ananananananananab");
		// String haystack = b.toString();
		// String needle = "ananananananananab";

		while (true) {
			String needle = null;
			String haystack = null;
			try {
				BufferedReader r = new BufferedReader(new InputStreamReader(
						System.in));
				needle = r.readLine();
				haystack = r.readLine();
			} catch (IOException e) {
				e.printStackTrace();
				System.exit(1);
			}
			int index = findIn(haystack, needle);
			if (index < 0) {
				System.out.println("NO");
			} else {
				System.out.println(index + 1);
			}
		}
		
		// System.out.println(haystack.substring(index, index+needle.length()));
		// System.out.println(haystack.substring(index - 6, index +
		// needle.length() + 6));
	}

}
